﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Web;

namespace EPlatform_web.App_Start
{
    public class Hpubilc
    {
        /// <summary>
        /// 获取MAC地址
        /// </summary>
        public static string GetMacValue()
        {
            string macAddress = "";
            try
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    if (!adapter.GetPhysicalAddress().ToString().Equals(""))
                    {
                        macAddress = adapter.GetPhysicalAddress().ToString();
                        for (int i = 1; i < 6; i++)
                        {
                            macAddress = macAddress.Insert(3 * i - 1, ":");
                        }
                        break;
                    }
                }
            }
            catch (Exception)
            {
                return "";
            }
            return macAddress;
        }

        /// <summary>
        /// 獲取IP地址
        /// </summary>
        public static string GetIpAddress()
        {
            try
            {
                string strHostName = System.Net.Dns.GetHostName();
                System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
                string localIp = ipEntry.AddressList[1].ToString();
                if (!string.IsNullOrEmpty(localIp) && IsIP(localIp))
                {
                    return localIp;
                }
                //string clientIp = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                //if (string.IsNullOrEmpty(clientIp))
                //{
                //    clientIp = HttpContext.Request.UserHostAddress;
                //}
                return "127.0.0.1";
            }
            catch (Exception)
            {
                return "";
            }
        }
        /// <summary>
        /// 驗證是否為IP格式
        /// </summary>
        public static bool IsIP(string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        /// <summary>
        /// 驗證登錄是否失效（用戶名session是否丟失）
        /// </summary>
        public static bool UserInvalid(object userObj)
        {
            try
            {
                if (userObj == null)
                {
                    return false;
                }
                else
                {
                    if (userObj.ToString().Length > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 查詢權限頁面
        /// </summary>
        public static DataSet GetPageInfo(string user, string currentPage)
        {
            try
            {
                string querySql = @"SELECT E.PAGE_NO, E.PAGE_NAME, E.PAGE_URL, E.PAGE_URL2
  FROM ifsapp.ETB_PAGE_INFO_TAB E, ifsapp.DOC_USER_TAB D
 WHERE D.CARD_NO = '{0}'
   AND INSTR(D.PAGE_URL2, E.PAGE_URL2) > 0
   AND E.PAGE_URL2 <> '{1}'
   AND E.MENU_GRADE = 1
 ORDER BY E.PAGE_NO";
                DataSet dataSet = OracleOperation.QueryData(string.Format(querySql, user, currentPage));
                return dataSet;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 查詢子菜單權限頁面
        /// </summary>
        public static DataSet GetPageInfoL2(string user, string currentPage)
        {
            try
            {
                string querySql = @"SELECT E.PAGE_NO, E.PAGE_NAME, E.PAGE_URL, E.PAGE_URL2
  FROM ifsapp.ETB_PAGE_INFO_TAB E
  LEFT JOIN ifsapp.ETB_PAGE_INFO_TAB E2
    ON E.PAGE_NO_P = E2.PAGE_NO
  LEFT JOIN ifsapp.DOC_USER_TAB D
    ON INSTR(D.PAGE_URL2, E.PAGE_URL2) > 0
 WHERE D.CARD_NO = '{0}'
   AND E2.PAGE_URL2 = '{1}'
   AND E.MENU_GRADE = 2
 ORDER BY E.PAGE_NO";
                DataSet dataSet = OracleOperation.QueryData(string.Format(querySql, user, currentPage));
                return dataSet;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public class InStr
        {
            public string In_str { get; set; }
        }
        public static string DecodeFrontendBase64(string jsonString)
        {
            try
            {
                InStr instr = JsonConvert.DeserializeObject<InStr>(jsonString);
                // 1. 将Base64字符串转换为字节数组
                byte[] data = Convert.FromBase64String(instr.In_str);

                // 2. 将字节数组解码为UTF-8字符串
                return Encoding.UTF8.GetString(data);
            }
            catch (FormatException)
            {
                // 处理无效的Base64格式
                //Console.WriteLine("错误：无效的Base64字符串");
                return "";
            }
        }
    }
}